import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class Booking
{
	public static int bookingid = 0;
	public static int facilityid = 0;
	public static int venueid = 0;
	public static int userid = 0;
	public static Date bookingdate = null;
	public static Date bookingstart = null;
	public static boolean new_record = false;

	public Booking(int uid, int fid, int vid, Date bs)
	{
		new_record = true;
		userid = uid;
		facilityid = fid;
		venueid = vid;
		bookingstart = bs;
	}

	public Booking(int bid) throws SQLException
	{
		new_record = false;
		bookingid = bid;

		Connector conn = new Connector("select * from booking where bookingid = '" + bid + "'");
		ResultSet rs = conn.execute();
		if(rs.first())
		{
			userid = rs.getInt("userid");
			venueid = rs.getInt("venueid");
			facilityid = rs.getInt("facilityid");
			String bs = rs.getString("bookingstart");
			try
			{
				DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				bookingstart = format.parse(bs);
			}
			catch (ParseException e)
			{
				e.printStackTrace();
			}
		}
		rs.close();
	}
	
	public static boolean insert() throws SQLException
	{
		if(new_record)
		{
			Timestamp tbs = new Timestamp(bookingstart.getTime());
			
			String sql = "insert into booking values(null,"
						+ userid + ","
						+ facilityid + ","
						+ venueid + ",'"
						+ tbs + "')";

			Connector conn = new Connector(sql);
			conn.execute_sql();
			
			return true;
		}
		else
			return false;
	}

	public static boolean delete() throws SQLException
	{
		if(!new_record)
		{
			String sql = "delete from booking where bookingid='" + bookingid + "'";

			Connector conn = new Connector(sql);
			conn.execute_sql();
			
			return true;
		}
		else
			return false;
	}

	public static void setUserId(int uid)
	{
		userid = uid;
	}

	public static void setFacilityId(int fid)
	{
		facilityid = fid;
	}

	public static void setVenueId(int vid)
	{
		venueid = vid;
	}

	public static void setBookingStart(Date ds)
	{
		bookingstart = ds;
	}

	public static int getUserId()
	{
		return userid;
	}

	public static int getFacilityId()
	{
		return facilityid;
	}

	public static int getVenueId()
	{
		return venueid;
	}

	public static Date getBookingStart()
	{
		return bookingstart;
	}
}
